import _ from "lodash";

export class DataLoader {
  private scrollContainer: any;
  private nextFlag: boolean;
  private paginatedData: {
    currentIndex: number,
    pageSize: number,
    totalPage: number,
    totalDataCount: number
  };
  private onLoadCallback?: (data: any) => void;

  constructor(pageSize: number, totalDataCount: number, scrollContainer: any, nextFlag: boolean, callbackFn?: (data: any) => void) {
    this.scrollContainer = scrollContainer;
    this.nextFlag = nextFlag;
    const totalPage = Math.ceil(totalDataCount / pageSize);
    // console.log("totalPage", totalPage);
    const modeVal = totalDataCount % pageSize;// 如果总条数不能整除每页条数，则说明最后一页无法撑满页面，需要将倒数第二页数据拿出来放在一页中，这样在组建中需要从倒数第三页开始计算
    const lastStartPage = modeVal === 0 ? 0 : 1;
    // 现在封装的逻辑是页面加载之后会默认加载一页的数据，要么是第一页要么是最后一页
    // 因此传递进来的总页数应该是从第二页或者倒数第二页开始
    const currentIndex = nextFlag ? 1 : totalPage - lastStartPage;

    this.scrollContainer.addEventListener("scroll", this.handleScroll);
    this.paginatedData = {
      currentIndex,
      pageSize,
      totalPage,
      totalDataCount
    };
    this.onLoadCallback = callbackFn;
  }

  destroy(): void {
    this.scrollContainer.removeEventListener("scroll", this.handleScroll);
  }

  updatePaginInfo(currentIndex: number, newTotalDataCount: number): void {
    const totalPage = Math.ceil(newTotalDataCount / this.paginatedData.pageSize);

    // 更新分页数据
    this.paginatedData = {
      currentIndex,
      pageSize: this.paginatedData.pageSize,
      totalPage,
      totalDataCount: newTotalDataCount,
    };
  }

  getInitData(): any {
    console.log("获取首页数据");
  }

  // 使用类属性箭头函数绑定 this
  private handleScroll = _.debounce(() => {
    if (!this.nextFlag) {
      this.loadPreviousPageData();
    } else {
      this.loadNextPageData();
    }
  }, 300); // 调整防抖时间为 300ms

  private loadPreviousPageData(): void {
    const isScrollingTop = (this.scrollContainer.scrollTop < 100);
    if (isScrollingTop) {
      if (this.paginatedData.currentIndex > 1) {
        this.paginatedData.currentIndex--;
        this.onLoadCallback?.(this.paginatedData);
        this.scrollContainer.scrollTop = 200;
      }
      console.log("加载上一页数据", this.paginatedData.currentIndex);
    }
  }

  private loadNextPageData(): void {
    const scrollHeight = this.scrollContainer.scrollHeight;
    const scrollTop = this.scrollContainer.scrollTop;
    const scrollClient = this.scrollContainer.clientHeight;
    const isScrollingBottom = (scrollHeight - scrollTop - scrollClient < 100);
    // console.log("scrollHeight", scrollHeight, "scrollTop", scrollTop, "scrollClient", scrollClient);
    if (isScrollingBottom) {
      this.paginatedData.currentIndex++;
      if (this.paginatedData.currentIndex <= this.paginatedData.totalPage) {
        this.onLoadCallback?.(this.paginatedData);
        this.scrollContainer.scrollTop = scrollHeight;
      }
    }
  }

  setScroll(): void {
    if (this.nextFlag) {
      const scrollHeight = this.scrollContainer.scrollHeight;
      this.scrollContainer.scrollTop = scrollHeight - 300 < 0 ? 0 : scrollHeight - 300;
    } else {
      const scrollHeight = this.scrollContainer.scrollHeight;
      console.log("this.paginatedData.currentIndex", this.paginatedData.currentIndex);
      this.scrollContainer.scrollTop = scrollHeight < 200 ? 0 : 200;
    }
  }
}
